import { createRouter, createWebHistory } from 'vue-router'

const routes = [  
  {
    path:'/',
    redirect:'/noteClass'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/register.vue'),
    meta: {
      title: '注册'
    }
  },
  {
    path: '/noteClass',
    name: 'noteClass',
    component: () => import('@/views/noteClass.vue'),
    meta: {
      title: '笔记分类'
    }
  },
  {
    path:'/noteList',
    name: 'noteList',
    component:()=>import('@/views/noteList.vue'),
    meta:{
      title: '笔记列表'
    }
  },
  {
    path:'/noteDetail',
    name: 'noteDetail',
    component:()=>import('@/views/noteDetail.vue'),
    meta:{
      title: '笔记详情'
    }
  },
  {
    path:'/notePublish',
    name: 'notePublish',
    component:()=>import('@/views/notePublish.vue'),
    meta:{
      title: '发布笔记'
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (!whiteList.includes(to.path)) {
    if (!sessionStorage.getItem('userInfo')) {
      router.push('/login')
      return
    }
    next()
    return
  }
  next()
})

export default router
